import { http } from '../../request/index'
Page({
  data: {
    leftMenList:[],
    rightContent:[],
    leftIndex:0
  },
  cates:[],
  onLoad: function (options) {
    //1.先判断本地有没有旧数据{time: Date.now(), data:[...]}
    ///2.没有就数据 发送请求
    //3. 有旧数据 同时没有过期就使用本地旧数据

    const Cates = wx.getStorageSync('cates')
    if(!Cates) {
      this.getCate()
    }else {
      if(Date.now() - Cates.time > 300000) {
        this.getCate()
      }else {
        this.cates = Cates.data;
        let leftMenList = this.cates.map( v => v.cat_name)
        let rightContent = this.cates[0].children
        this.setData({
          leftMenList,
          rightContent
        })
      }
    }
    
  },
  async getCate() {
    // http({
    //   url:'/categories'
    // })
    // .then(res => {
    //   this.cates = res.data.message;

    //   //本地存储数据
    //   wx.setStorageSync('cates', {time: Date.now(), data: this.cates})

    //   let leftMenList = this.cates.map( v => v.cat_name)
    //   let rightContent = this.cates[0].children
    //   this.setData({
    //     leftMenList,
    //     rightContent
    //   })
    // })
    const res = await http({url:"/categories"})
    this.cates = res;
    //本地存储数据
    wx.setStorageSync('cates', {time: Date.now(), data: this.cates})

    let leftMenList = this.cates.map( v => v.cat_name)
    let rightContent = this.cates[0].children
    this.setData({
      leftMenList,
      rightContent
    })
  },
  menuClick(e) {
    let {index} = e.currentTarget.dataset;
    let rightContent = this.cates[index].children
    this.setData({
      leftIndex:e.currentTarget.dataset.index,
      rightContent
    })
  }
})